package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollUtil;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.service.mongo.RecommendUserService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserServiceImpl implements RecommendUserService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public RecommendUser findTodayBest(Long toUserId) {
        //1.创建条件对象(条件,得分排序,分页)
        Query query = new Query(Criteria.where("toUserId").is(toUserId))
                .with(Sort.by(Sort.Order.desc("score")))
                .skip(0).limit(1);

        //2.查询推荐用户表,返回推荐用户
        return mongoTemplate.findOne(query,RecommendUser.class);
    }

    @Override
    public PageBeanVo findByPage(Long toUserId, Integer pageNum, Integer pageSize) {
        //1.创建条件对象(登陆者id,排序,分页)
        Query query = new Query(Criteria.where("toUserId").is(toUserId))
                .with(Sort.by(Sort.Order.desc("score")))
                .skip((pageNum-1)*pageSize+1).limit(pageSize);

        //2.查询推荐用户表返回list
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);

        //3.封装pageBeanVo且返回
        Long counts = CollUtil.isEmpty(recommendUsers)?0L:recommendUsers.size();
        return new PageBeanVo(pageNum,pageSize,counts,recommendUsers);
    }

    @Override
    public RecommendUser findPersonalInfo(Long toUserId, Long recommendUserId) {
        //1.创建条件对象(俩id)
        Query query = new Query(Criteria.where("toUserId").is(toUserId).and("userId").is(recommendUserId));
        //2.查询推荐表,返回推荐用户
        return mongoTemplate.findOne(query,RecommendUser.class);
    }
}
